package org.xiaom.butler.activity;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.xiaom.butler.R;
import org.xiaom.butler.bean.Content;
import org.xiaom.butler.bean.Department;
import org.xiaom.butler.dbService.ContentService;
import org.xiaom.butler.util.Util;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ItemDetailActivity extends Activity {
	ContentService contentService;
	List<Content> contents;
	Department d;
	List<String> names = new ArrayList<String>();
	List<String> contentStrs = new ArrayList<String>();
	TextView chName;
	TextView engName;
	ImageView image;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initData();
		setContentView(R.layout.item_detail);
		findViewById(R.id.btn_show_card).setOnClickListener(
				new OnClickListener() {
					public void onClick(View v) {
						Intent intent=new Intent(ItemDetailActivity.this,CardDetailActivity.class);
						intent.putExtra("department", d);
						startActivity(intent);
					}
				});
		chName=(TextView) findViewById(R.id.txt_detail_title_ch);
		engName=(TextView) findViewById(R.id.txt_detail_title_eng);
		image=(ImageView) findViewById(R.id.img_detail);
		chName.setText(d.getChName());
		engName.setText(d.getEngName());
		image.setBackgroundDrawable(Drawable.createFromPath(d.getImagePath()));
		ListView lv = (ListView) findViewById(R.id.list_content_item);
		lv.setAdapter(getAdapter());
		setListViewHeightBasedOnChildren(lv);
	}

	private ListAdapter getAdapter() {
		return new BaseAdapter() {
			public View getView(int position, View convertView, ViewGroup parent) {
				View root=getLayoutInflater().from(ItemDetailActivity.this).inflate(R.layout.item_detail_item,parent,false);
				TextView name=(TextView) root.findViewById(R.id.txt_content_name);
				TextView content=(TextView) root.findViewById(R.id.txt_content_content);
				name.setText(names.get(position));
				content.setText(contentStrs.get(position));
				return root;
			}
			public long getItemId(int position) {
				return position;
			}
			public Object getItem(int position) {
				return position;
			}
			public int getCount() {
				return names.size();
			}
		};
	}

	private void initData() {
		contentService = new ContentService(this);
		d = (Department) getIntent().getExtras().get("department");
		contents = contentService.getByDepartment(d.getId().toString());
		for (int i = 0; i < contents.size(); i++) {
			Content c = contents.get(i);
			String name = c.getChName();
			String con = c.getChContent();
			if (!Util.isBlank(name)) {
				names.add(name);
				contentStrs.add(con);
			}
			name = c.getEngName();
			con = c.getEngContent();
			if (!Util.isBlank(name)) {
				names.add(name);
				contentStrs.add(con);
			}
		}

	}

	public void setListViewHeightBasedOnChildren(ListView listView) {  
	    ListAdapter listAdapter = listView.getAdapter();   
	    if (listAdapter == null) {  
	        return;  
	    }  

	    int totalHeight = 0;  
	    for (int i = 0; i < listAdapter.getCount(); i++) {  
	        View listItem = listAdapter.getView(i, null, listView);  
	        listItem.measure(0, 0);  
	        totalHeight += listItem.getMeasuredHeight();  
	    }  

	    ViewGroup.LayoutParams params = listView.getLayoutParams();  
	    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
	    ((MarginLayoutParams)params).setMargins(10, 10, 10, 10);
	    listView.setLayoutParams(params);  
	} 
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		this.finish();
		return super.onOptionsItemSelected(item);
	}
}
